package com.sanyang.lawyer.controller.system;

import com.sanyang.lawyer.entity.System_authority;
import com.sanyang.lawyer.entity.System_role;
import com.sanyang.lawyer.service.system.AuthorityService;
import com.sanyang.lawyer.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: lawyer
 * @description: 权限控制层
 * @author: Mis.Feng
 * @create: 2019-10-15 17:08
 **/
@RestController
@RequestMapping("aut")
public class AutController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthorityService authorityService;
    //遍历所有权限生成ztree
    @RequestMapping("listQx")
    public List<System_authority> listQx(){
        List<System_authority> list=authorityService.select();
        return list;
    }
    //根据所选角色在ztree中选中拥有的权限
    @RequestMapping("updateAut")
    public List<System_authority> updateAut(int tableRoleid){
        List<System_authority> list=authorityService.updateAut(tableRoleid);
        return list;
    }
    //修改权限
    @RequestMapping("insertAut")
    public String insertAut(String arr,int tableRoleid){
        authorityService.deleteRoleAut(tableRoleid);
        System.out.println("123");
        String[] a=arr.split(",");
        for(int i=0;i<a.length;i++){
            authorityService.insertAut(tableRoleid,Integer.parseInt(a[i]));
        }
        return "200";
    }
    @RequestMapping("insRole")
    public void insRole(String roleName,String roleDesc,String arr){
        System_role role=new System_role();
        role.setRoleName(roleName);
        role.setRoleDesc(roleDesc);
        roleService.insert(role);
        int roleid=roleService.getByName(roleName);
        String[] a=arr.split(",");
        for(int i=0;i<a.length;i++){
            authorityService.insertAut(roleid,Integer.parseInt(a[i]));
        }
    }
    @RequestMapping("delRole")
    public void delRole(int roleId){
        roleService.delete(roleId);
    }

    @RequestMapping("insRoleName")
    public int  insRoleName(String roleName){
        Integer i=roleService.getByroleName(roleName);
        System.out.println(i);
        if(i!=0){
            return 200;
        }else {
            return 100;
        }
    }
}
